/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.mto.module.karaoke.service.impl;

import java.util.List;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.mto.module.karaoke.model.Song;
import com.mto.module.karaoke.model.UserSongs;
import com.mto.module.karaoke.portlet.util.comparator.KaraokeComparator;
import com.mto.module.karaoke.service.SongLocalServiceUtil;
import com.mto.module.karaoke.service.base.UserSongsLocalServiceBaseImpl;

/**
 * The implementation of the user songs local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link com.mto.module.karaoke.service.UserSongsLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 * </p>
 *
 * @author vuonglm
 * @see com.mto.module.karaoke.service.base.UserSongsLocalServiceBaseImpl
 * @see com.mto.module.karaoke.service.UserSongsLocalServiceUtil
 */
public class UserSongsLocalServiceImpl extends UserSongsLocalServiceBaseImpl {
	/*
	 * NOTE FOR DEVELOPERS:
	 *
	 * Never reference this interface directly. Always use {@link com.mto.module.karaoke.service.UserSongsLocalServiceUtil} to access the user songs local service.
	 */
	
	public UserSongs createUserSongs(UserSongs userSongs) throws SystemException, PortalException {
		userSongsLocalService.addUserSongs(userSongs);
		Song song = SongLocalServiceUtil.getSong(userSongs.getSongId());
		song.setRecordNumber(1 + song.getRecordNumber());
		songLocalService.updateSong(song);
		return userSongs;
	} 
	
	public List<UserSongs> search(UserSongs userSongs, int start, int end, String orderByCol, String orderByType) throws SystemException {
		OrderByComparator orderByComparator = new KaraokeComparator(orderByCol, orderByType);
		long uid = userSongs.getUserId();
		long sid = userSongs.getSongId();
		if(uid > 0) {
			if(sid > 0) return userSongsPersistence.findByU_S(uid, sid, start, end, orderByComparator);
			return userSongsPersistence.findByUserId(uid, start, end, orderByComparator);
		}
		if(sid > 0) {
			return userSongsPersistence.findBySongId(sid, start, end, orderByComparator);
		}
		return userSongsPersistence.findAll(start, end, orderByComparator);
	}
	
	public Integer searchCount(UserSongs userSongs) throws SystemException {
		long uid = userSongs.getUserId();
		long sid = userSongs.getSongId();
		if(uid > 0) {
			if(sid > 0) return userSongsPersistence.countByU_S(uid, sid);
			return userSongsPersistence.countByUserId(uid);
		}
		if(sid > 0) {
			return userSongsPersistence.countBySongId(sid);
		}
		return userSongsPersistence.countAll();
	}
}